using UnityEngine;

public class PaddleControll : MonoBehaviour
{
	public Transform racket;

	public CameraController cam;

	public Vector3 cameraDisplace;

	public int side = -1;

	protected bool buttonDown;

	protected Plane movePlane;

	private float tableTopY = 1f;

	public Ball ball;

	public void Init()
	{
		movePlane = new Plane(Vector3.up, Vector3.up * tableTopY);
	}

	private void Awake()
	{
		Init();
	}

	private void Update()
	{
		if (Input.GetKeyDown(KeyCode.Escape))
		{
			BehaviourSingleton<ScreenNavigation>.instance.LoadMain(MenuParameters.MenuParams.ShowScreen.None);
		}
		else
		{
			if (!InputControl.mouseButton)
			{
				return;
			}
			Camera component = cam.GetComponent<Camera>();
			Ray ray = component.ScreenPointToRay(Input.mousePosition);
			float enter = 0f;
			if (movePlane.Raycast(ray, out enter))
			{
				Vector3 point = ray.GetPoint(enter);
				if (Mathf.Sign(point.z) != (float)side)
				{
					point.z = 0f;
				}
				racket.position = point;
				racket.rotation = Quaternion.AngleAxis(Mathf.Sign(point.x) * Mathf.LerpAngle(0f, -60f, Mathf.Abs(point.x) / 0.5f), Vector3.forward);
				Vector3 vector = new Vector3(point.x * 0.3f, cameraDisplace.y, cameraDisplace.z);
				cam.SetTarget(vector, Quaternion.LookRotation(Vector3.up * tableTopY - vector), cam.initialFOV);
				if (Input.GetMouseButtonDown(0))
				{
					ball.cachedTransform.position = racket.position;
					ball.MakeKinematic();
					Vector3 landingPos = new Vector3(0f, Table.tabletopy, (float)(-side) * Table.halfLength * 0.5f);
					ball.HitBall(landingPos, Table.netHeight, 0.5f, 10f, 0f, null);
				}
			}
		}
	}
}
